package com.slkj.shilixiaoyuanapp.ui.teacher.tool.schoolmuseum;

import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.StudentWorksEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.service.ToolService;
import com.slkj.shilixiaoyuanapp.view.DividerGridItemDecoration;
import com.slkj.shilixiaoyuanapp.view.GridItemDecoration;
import com.slkj.shilixiaoyuanapp.view.MarginItemDecoration;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;

@ActivityInfo(layout = R.layout.activity_school_meseum,title = "校园博物馆")
public class SchoolMuseumActivity extends BaseActivity{
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @BindView(R.id.recycler)
    RecyclerView recyclerView;
    BaseQuickAdapter baseQuickAdapter;
    public List<StudentWorksEntity.ModuleBean> datas=new ArrayList();
    private List<StudentWorksEntity> imgs=new ArrayList<>();
    @BindView(R.id.rl_schoolbg)
    RelativeLayout rl_schoolbg;
    @BindView(R.id.iv_schoolbg)
    ImageView iv_schoolbg;
    String img;


    @Override
    protected void init() {
        recyclerView.setLayoutManager(new GridLayoutManager(this,2));
        //recyclerView.addItemDecoration(new MarginItemDecoration(20,2));
        recyclerView.addItemDecoration(new GridItemDecoration(this));
        //recyclerView.addItemDecoration(new DividerGridItemDecoration(this));
        baseQuickAdapter = new BaseQuickAdapter<StudentWorksEntity.ModuleBean, BaseViewHolder>(R.layout.item_student_works, datas) {
            @Override
            protected void convert(BaseViewHolder helper, final StudentWorksEntity.ModuleBean item) {
                helper.setText(R.id.tv_worksname,item.getName());
                Glide.with(SchoolMuseumActivity.this).load(item.getImg()).asBitmap().centerCrop().into((ImageView) helper.getView(R.id.iv_worksbg));
                if (helper.getAdapterPosition()%4==0){
                    helper.setBackgroundColor(R.id.rl_worksnamebg, Color.parseColor("#0087FC"));
                }else if (helper.getAdapterPosition()%4==1){
                    helper.setBackgroundColor(R.id.rl_worksnamebg, Color.parseColor("#FED504"));
                }else if (helper.getAdapterPosition()%4==2){
                    helper.setBackgroundColor(R.id.rl_worksnamebg, Color.parseColor("#FF6953"));
                }else {
                    helper.setBackgroundColor(R.id.rl_worksnamebg, Color.parseColor("#5DE2C8"));
                }
            }

        };
        baseQuickAdapter.setOnItemClickListener((adapter, view, position) -> {
            StudentWorksEntity.ModuleBean item = datas.get(position);
            StudentsWorksActivity__JumpCenter.builder(this)
                    .setType(item.getType())
                    .setTitle(item.getName())
                    .create().go();
        });
        recyclerView.setAdapter(baseQuickAdapter);
//        View view = getLayoutInflater().inflate(R.layout.head_museum, (ViewGroup) recyclerView.getParent(), false);
//        rl_schoolbg=view.findViewById(R.id.rl_schoolbg);
//        iv_schoolbg=view.findViewById(R.id.iv_schoolbg);
//        baseQuickAdapter.addHeaderView(view);
        getData();
        stateLayout.setOnReLoadListener(this::getData);
        rl_schoolbg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StudentWorksEntity item=imgs.get(0);
                PanoramaActivity__JumpCenter.builder(SchoolMuseumActivity.this)
                        .setImg(item.getImgs())
                        .create().go();
            }
        });
    }

    private void getData() {
        ToolService service= HttpHeper.get().toolService();
        service.schoolMuseum().compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<StudentWorksEntity>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(StudentWorksEntity back) {
                        imgs.add(back);
                        //img=back.getImgs();
                        Glide.with(SchoolMuseumActivity.this).load(back.getImgs()).asBitmap().centerCrop().into(iv_schoolbg);
                        datas.addAll(back.getModule());
                        baseQuickAdapter.notifyDataSetChanged();
                    }
                });
    }
}
